<?php
declare (strict_types=1);

namespace app\api\controller;

use app\api\controller\Base;
use app\common\model\Advert;
use app\common\model\Danye;
use app\common\model\Goods;
use app\common\model\GoodsList;
use app\common\model\OrderList;
use app\common\model\User;
use app\common\model\Yushou;
use think\facade\Db;
use \think\Request;


class Index extends Base
{
    //首页
    public function index(Request $request)
    {
        #首页轮播图
        $advert = Advert::field('imgurl,ttype,coupon_id,goods_id,title')->where(['type' => 1])->order('sort desc,id desc')->select();
        foreach ($advert as &$advert_value) {
            $advert_value['imgurl'] = imageUrl($advert_value['imgurl']);
        }
        #公告
        $notice = Danye::field('title,content')->where(['id' => 3])->find();
        $notice['content'] = contentUrl($notice['content']);
        
        #客服
        $rule = getConfig('base');
        $other = [
            'is_shou_tan' => $rule['is_shou_tan'],
            'jump_appid' => $rule['jump_appid'],
            'corpid' => $rule['corpid'],
            'wx_link' => $rule['wx_link'],
            'erweima' => imageUrl($rule['erweima']),
        ];
        return $this->renderSuccess('请求成功', compact('advert', 'notice', 'other'));
    }
    
    //首页菜单
    public function menu(Request $request)
    {
        $user = $this->getUser(false);
        $whe = [];
        $whe[] = ['order_type', '=', 2];
        $menu = [
            ['id' =>1, 'title'=> '一番赏'],
            ['id'=> 2,  'title' =>'无限赏'],
            ['id'=> 8, 'title'=> '领主赏'],
            ['id'=> 9, 'title'=> '连击赏'],
            ['id'=> 3, 'title' => '擂台赏'],
            ['id'=> 6, 'title'=> '全局赏']
        ];
        if (!empty($user)) {
            $whe[] = ['user_id', '=', $user['id']];
        }
        $check_order = OrderList::where($whe)->find();
        if (empty($check_order) ||  $user == false) {
            $data = [$menu[0]];
        } else {
            $data = $menu;
        }
        $data = $menu;
        return $this->renderSuccess('请求成功', compact('data'));
    }
    
    //获取音效
    public function get_yinxiao()
    {
        $base = getConfig('base');
        $yinyue = '/'.$base['yinyue'];
        $yinxao = imageUrl($yinyue);
        return $this->renderSuccess('请求成功', $yinxao);
    }

    /**
     * 预售日历
     */
    public function yushourili()
    {
        $data = Yushou::field('id,title,imgurl,sale_time,goods_id')->order('sort desc,id desc')->limit(10)->select()->toArray();
        foreach ($data as &$value) {
            $goods = Goods::field('id')
                ->where('id', '=', $value['goods_id'])
                ->where('status', 'in', [1, 3])
                ->find();
            if (!$goods) {
                Yushou::field('goods_id')->where('id', '=', $value['id'])->update(['goods_id' => 0]);
                $value['goods_id'] = 0;
            }
            $value['imgurl'] = imageUrl($value['imgurl']);
            $value['month'] = date('m月', $value['sale_time']);
            $value['day'] = date('d', $value['sale_time']);
        }
        return $this->renderSuccess("请求成功", $data);
    }

    /**
     * 单页
     */
    public function danye()
    {
        $type = \request()->param('type/d', 0);
        $info = Danye::where(['id' => $type])->find();
        if ($info) {
            $content = contentUrl($info['content']);
        } else {
            $content = '';
        }
        return $this->renderSuccess("请求成功", $content);
    }



}
